/**
 * 词频
 */
var Wechat = require('../../models/wechat/scrapyModel').Wechat;
var WechatStat = require('../../models/wechat/statModel').WechatStat;
var Keyword = require('../../models/keywordsModel').Keyword;
var Util = require('../../util/util');
var Spare = require('../../util/spareData');


//global variate
var fHash = new Util.HashMap();
fHash = initFilter();


function main(){
    console.log('微信 词频 开始');
    FindAllKeywords();
}

function initFilter(){
    //过滤词表 hash赋值
    console.log('过滤词表映射');
    var filterKeywords = Spare.wechatSpare.filterKeywords;
    var fHash = new Util.HashMap();
    for (var i in filterKeywords) {
        fHash.Set(filterKeywords[i], filterKeywords[i]);
    }
    return fHash;
}

function FindAllKeywords(){
    Keyword.findAllKeywords(function (err, docs) {
        var list = [];
        if (err) {
            console.log(err);
        }
        for (var i in docs) {
            list.push({
                'keyword_id': docs[i]._id,
                'title': docs[i].title
            })
        }
        ListForEach(list,function(){
            console.log('===============  微信 词频 统计 所有完成了！！');
        })

    });
}

function ListForEach(list,callback){
    var i,getTitle,findByTitle,filterWord,initExport,insertStat,next,done;
    i = 0;

    getTitle = function(){
        if(i>=list.length){
            return done();
        }
        console.log('获取题目...'+list[i].title);
        findByTitle(list[i]);
    };

    findByTitle = function(list){
        Wechat.findByKeyword(list.title,function(err,data){
            console.log('获取'+list.title+'的数据长度....'+data.length);
            filterWord(list,data);
        });
    };

    filterWord = function(list,data){
        console.log('过滤词组...'+list.title);
        var frHash = new Util.HashMap(),tempData={};

        for(var d in data){
            var keywords = data[d].keywords,
                len = keywords.length, a;
            if (keywords != '') {
                for (a = 0; a < len; a++) {
                    //filter
                    if (!fHash.Contains(keywords[a])) {
                        //stat
                        if(!frHash.Contains(keywords[a])){
                            //not exist
                            frHash.Set(keywords[a],keywords[a]);
                            tempData[keywords[a]] = {
                                'word':keywords[a],
                                'frequency':1
                            };
                        }
                        else{
                            //exist
                            if(tempData[keywords[a]]){
                                tempData[keywords[a]].frequency++;
                            }

                        }
                    }
                }
            }
        }
        initExport(list,tempData);
    };

    initExport = function(list,tempData){
        console.log('组装输出...'+list.title);
        var Temp = {
            "words":[]
        },exportData=[];
        //init tempData
        for(var t in tempData){
            Temp.words.push({
                'word':tempData[t].word,
                'frequency':tempData[t].frequency
            })
        }
        //console.log(Temp);
        exportData.push(Temp);
        insertStat(list,exportData);
    };

    insertStat = function(list,exportData){
        //console.log(exportData);
        WechatStat.saveFrequency(list.keyword_id,list.title,exportData);
        next(list);
    };

    next = function(list){
        console.log('已经完成啦....'+list.title);
        ++i;
        getTitle();
    };

    done = function(){
        callback();
    };

    return getTitle();
}
exports.main = main;

